/**
 * Created by zkt on 2017/1/17.
 */
var menuModel = require('../model/Menu');
var util = require("util");
module.exports = function(app){
    app.get('/', function (req, res) {
        res.render('login');
    });
    app.post('/login', function (req, res) {
        var username = req.body.username;
        var pwd = req.body.password;
        req.session.user = {username:username};
        var menuList = req.session.menuList;
        if(util.isNullOrUndefined(menuList)){
            var menuArray = [];
            var subMenuArray = [];
            menuModel.getAllList({},function (error,result) {
                if(error){
                    console.log('查询出错！');
                }else{
                    if(result){
                        for(var i = 0 ; i < result.length;i++){
                            var menu = result[i];
                            if(util.isNullOrUndefined(menu.menu_pid) || menu.menu_pid == 0){
                                menuArray.push(menu);
                            }else{
                                subMenuArray.push(menu);
                            }
                        }
                    }
                }
                req.session.menuList = {menuArray:menuArray,subMenuArray:subMenuArray};
                app.locals.menuArray = menuArray;
                app.locals.subMenuArray = subMenuArray;
                res.redirect('/index');
            });
        }else{
            app.locals.menuArray = menuList.menuArray;
            app.locals.subMenuArray = menuList.subMenuArray;
            res.redirect('/index');
        }
    });
};

